#include "datamanager.h"
#include "Freya_Define.h"
#include "../FreyaLaunch/Freya_Global.h"

#include "freyacommon.h"
#include "SqlDatabase.h"
#include "dataoperate.h"
#include "systemlog.h"

QString ModuleInfo = QString("{ \"LaunchKey\": \"Freya_6\", \"ModuleName\": \"DataManager\", \"ModuleVerison\": \"%1\" }").arg(FREYAMODULE_VER);
FRYLAC_FUN_LAUNCHPORT(DataManager, DATAMANAGERSHARED_EXPORT, ModuleInfo.toUtf8().constData())

DataManager::DataManager() :
    FreyaBaseAction(FreyaBaseControl::GetFreyaControl(), "DataManager")
{
    RegisterCommands();
}

DataManager::~DataManager()
{
    m_DataBase->deleteLater();
}

void DataManager::Execute(const FreyaData data)
{
    auto VerResponse = [&]{
        FreyaData pData = FreyaBaseData::CreateData(FRYLAC_CMD_VERRESPONSE);
        QVariantMap verMap;
        verMap.insert("DataManager", FREYAMODULE_VER);
        pData->SetArgument(verMap);
        FREYA_REQUESTEXECUTION(pData);
    };

    FREYACONNECT(FRYLAC_CMD_HELLOFREYA, VerResponse());

    FREYACONNECT(FGP_CMD_HELLOWORLD, {qInfo () << "DataManager : Received Hello!";});

    FREYACONNECT(FGP_CMD_DATAOPERATE, { DataModuleOperate(data); });
    FREYACONNECT(FGP_CMD_SYSTEMLOGRECORD, { LogRecord(data); });
}

void DataManager::InitConfig()
{
    qInfo() << "InitConfig";
    FreyaBaseControl::GetFreyaControl()->GetConfigFromFile(MODULECONFIGDIR "/semen.json");
}

void DataManager::InitDataBase()
{
    qInfo() << "InitDataBase";

    QString host = FreyaBaseControl::GetFreyaControl()->GetConfig(QStringList() << "semen_custom" << "DataBase" << "Address").toString();
    int port = FreyaBaseControl::GetFreyaControl()->GetConfig(QStringList() << "semen_custom" << "DataBase" << "Port").toInt();
    QString user = FreyaBaseControl::GetFreyaControl()->GetConfig(QStringList() << "semen_custom" << "DataBase" << "UserName").toString();
    QString pass = FreyaBaseControl::GetFreyaControl()->GetConfig(QStringList() << "semen_custom" << "DataBase" << "Password").toString();
    QString name = FreyaBaseControl::GetFreyaControl()->GetConfig(QStringList() << "semen_custom" << "DataBase" << "DBName").toString();
    m_DataBase = new SqlDatabase();
    if(!m_DataBase->InitDataBase(host, port, user, pass, name))
    {
        QString CreateDB = FreyaBaseControl::GetFreyaControl()->GetConfig(QStringList() << "semen_custom" << "DataBase" << "CreateDB").toString();
        qInfo() << "CreateSqlDataBase" << m_DataBase->CreateSqlDataBase(CreateDB);
    }
    if(m_DataBase->isValid())
    {
        qInfo() << "InitDBTables" << m_DataBase->InitDBTables();
    }
    else
    {
        qWarning() << "DataBase is invalid";
    }
}

void DataManager::ModuleLaunch()
{
    // 在自有线程初始化，避免数据库访问的线程问题
    InitConfig();
    InitDataBase();
}

void DataManager::ModuleQuit()
{
    m_DataBase->DisDatabaseConnect();
}

void DataManager::DataModuleOperate(const FreyaData data)
{
    DataOperate dataOpera;
    dataOpera.FromVariant(data->GetArgument());
    dataOpera.OperaResult = m_DataBase->DataBaseOperate(dataOpera);
    data->SetArgument(dataOpera.ToVariant());
    FREYA_REPLYEXECUTION(data);
}

void DataManager::LogRecord(const FreyaData data)
{
    SystemLog log;
    log.FromVariant(data->GetArgument());
    QVariant OperaRet;
    bool LogRecordResult = false;
    FREYA_DATAOPERATE(DataOperate::e_SystemLogRecord, log.ToVariant(), LogRecordResult, OperaRet);
    if(!LogRecordResult)
    {
        qWarning() << "DataManager::LogRecord failed!";
    }
}
